package sim;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import object.connector.Spring;
import object.mass.Mass;

/**
 * A class to manage assemblies, sets of Masses and associated
 * Springs.
 * 
 * @author Niel Lebeck and Wynne Lok
 *
 */
public class Assembly {
    private List<Mass> myMasses;
    private List<Spring> mySprings;
    private Simulation mySim;

    /**
     * 
     * @param s Simulation
     */
    public Assembly(Simulation s) {
        mySim = s;
        myMasses = new ArrayList<Mass>();
        mySprings = new ArrayList<Spring>();
    }

    /**
     * Add a Mass to the assembly.
     * @param mass Mass
     */
    public void add (Mass mass) {
        myMasses.add(mass);
    }

    /**
     * Add a spring to the assembly.
     * @param spring Spring
     */
    public void add (Spring spring) {
        mySprings.add(spring);
    }

    /**
     * Get the list of Masses, to be used by the Simulation class
     * when doing calculations on multiple Assemblies.
     * @return
     */
    public List<Mass> getMassList() {
        return myMasses;
    }

    /**
     * Get the Mass with the given ID
     * @param id the ID of the mass
     * @return
     */
    public Mass getMass (int id) {
        for (Mass m : myMasses) {
            if (m.match(id)) {
                return m;
            }
        }
        return null;
    }

    /**
     * Paint the Masses and Springs in this Assembly.
     * @param pen Graphics
     */
    public void paint (Graphics2D pen) {
        for (Spring s : mySprings) {
            s.paint(pen);
        }
        for (Mass m : myMasses) {
            m.paint(pen);
        }
    }

    /**
     * Update the objects in this Assembly.
     * @param dt the amount of time that has passed
     */
    void updateMovers (double dt) {
        for (Spring s : mySprings) {
            s.update(mySim, dt);
        }
        for (Mass m : myMasses) {
            m.update(mySim, dt);
        }
    }
}
